<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Models\TopicBbs;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Category;

class SectionsController extends Controller
{
    //文章版块界面
    public function section_list_view(Request $request,$id)
    {
        //1、查询数据库总条数
        $count = count(TopicBbs::where('section',$id)->get());

    //2、设置每页显示条数
        $rev = '16';
    //3、求总页数
        $sums = ceil($count / $rev);
    //4、求单前页
        $page = $request->input('pn');
        if (empty($page)) {
            $page = "1";
        }
    //5、设置上一页、下一页
        $prev = ($page - 1) > 0 ? $page - 1 : 1;
        $next = ($page + 1) < $sums ? $page + 1 : $sums;
    //6、求偏移量
        $offset = ($page - 1) * $rev;
    //7、sql查询数据库
        @$data = TopicBbs::where('section',$id)->offset($offset)->limit($rev)->with('profile', 'tags')->get();
    //8、数字分页
        $pp = array();
        for ($i = 1; $i <= $sums; $i++) {
            $pp[$i] = $i;
        }
        return view('home/SectionsViews/Section_list', ['data' => $data, 'prev' => $prev, 'next' => $next, 'sums' => $sums, 'pp' => $pp, 'page' => $page,'section_id'=>$id]);
    }
    //文章点击翻页
    public function sectionList_change(Request $request)
    {
        error_reporting(0);
        $id=$request->input('id');
        $count = count(TopicBbs::where('section',$id)->get());
        $rev = '16';
        $sums = ceil($count / $rev);
        $page = $request->input('pn');
        if (empty($page)) {
            $page = "1";
        }
        $prev = ($page - 1) > 0 ? $page - 1 : 1;
        $next = ($page + 1) < $sums ? $page + 1 : $sums;
        $offset = ($page - 1) * $rev;
        $data = TopicBbs::where('section',$id)->offset($offset)->limit($rev)->with('profile', 'tags')->get();
        $pp = array();
        for ($i = 1; $i <= $sums; $i++) {
            $pp[$i] = $i;
        }
        return view('home/SectionsViews/Section_change', ['data' => $data, 'prev' => $prev, 'next' => $next, 'sums' => $sums, 'pp' => $pp, 'page' => $page,'section_id'=>$id]);

    }
    //文章滚动翻页
    public function sectionList_add(Request $request)
    {
        error_reporting(0);
        $id = $request->input('id');
        $rev = '8';
        $page = $request->input('pn');
        if (empty($page)) {
            $page = "1";
        }
        $offset = ($page - 1) * $rev;
        if (($offset + 8) * $page > 16 * $page) {
            return response()->json(array(
                'msg'=>'ok'
            ));
        }
        $data = TopicBbs::where('section',$id)->offset($offset)->limit($rev)->with('profile', 'tags')->get();
        return view('home/SectionsViews/Section_add', ['data' => $data]);

    }
    //文章分类界面
    public function section_list_best_view($id,$class)
    {
        return view('home/SectionsViews/Section_list_class', ['class'=>$class,'section_id'=>$id]);
    }
    //文章分类方法
    public function sectionList_class_add(Request $request)
    {
        error_reporting(0);
        $class=$request->input('sec_class');
        $id = $request->input('id');
        $rev = '8';
        $page = $request->input('pn');
        if (empty($page)) {
            $page = "1";
        }
        $offset = ($page - 1) * $rev;
        if($class=='best'){
            $data = TopicBbs::where('section',$id)->where('best',1)->offset($offset)->limit($rev)->with('profile', 'tags')->get();
        }
        if($class=='reply'){
            $data = TopicBbs::where('section',$id)->offset($offset)->limit($rev)->orderBy('reply_number','desc')->with('profile', 'tags')->get();
        }

        if($class=='vote'){
            $data = TopicBbs::where('section',$id)->offset($offset)->limit($rev)->orderBy('vote_number','desc')->with('profile', 'tags')->get();
        }
        if($class=='time'){
            $data = TopicBbs::where('section',$id)->offset($offset)->limit($rev)->orderBy('created_at','desc')->with('profile', 'tags')->get();
        }
        if($class=='all'){
            $data = TopicBbs::where('section',$id)->offset($offset)->limit($rev)->with('profile', 'tags')->get();
        }
        return view('home/SectionsViews/Section_add', ['data' => $data]);

    }
    //文章分类滚动
    public function sectionList_change2(Request $request)
    {
        error_reporting(0);
        $id = $request->input('id');
        $rev = '8';
        $page = $request->input('pn');
        if (empty($page)) {
            $page = "1";
        }
        $offset = ($page - 1) * $rev;
        if (($offset + 6) * $page > 12 * $page) {
            return;
        }
        $data = TopicBbs::where('section',$id)->offset($offset)->limit($rev)->with('profile', 'tags')->get();
        return view('home/SectionsViews/Section_add', ['data' => $data]);

    }

//    分类树
    public function section_list(Request $request){
        $originCategory=Category::orderBy('path')->get();
        $category=$this->sortOut($originCategory);
        $category2=[];
        foreach ($category as $c){
            $category2[]=$c['childern'];
        }
        $category3=[];
        foreach ($category2 as $a){
            foreach ($a as $z){
                $category3[]=$z['childern'];
            }
        }
        return view('home/SectionsViews/list')->with('category',$category)->with('category3',$category3)->with('category2',$category2);
    }
    //分类树方法
    private function sortOut($originCategory,$id=0){
        $categories=[];
        foreach ($originCategory as $o){
            if ($o->parent_id == $id){
                $children = $this->sortOut($originCategory,$o->id);
                $o->childern=$children;
                $categories[]=$o->toArray();
            }
        }
        return $categories;
    }
}
